﻿
Imports System.Reflection
Imports System.Globalization
Imports System.ComponentModel
Imports System.Data.SqlClient

Imports System.IO
Imports System.Collections.Generic

Imports DataAccessLayer
Imports Gurusoft.Components
Imports Microsoft.ApplicationBlocks.Data

Public Class frmOut_Transcript

    ' Fields
    Private m_sPrename As String = String.Empty
    Private m_sName As String = String.Empty
    Private m_sSname As String = String.Empty

    Private m_sPrename2 As String = String.Empty
    Private m_sName2 As String = String.Empty
    Private m_sSname2 As String = String.Empty
    Private m_sID As String = String.Empty

    ' Function 
    Private Function GetConnection() As SqlClient.SqlConnection
        Dim connection As New SqlConnection

        Try
            Dim sConnectionString As String = Transfer.My.MySettings.Default.TransferConnectionString.ToString()
            connection = New SqlClient.SqlConnection(sConnectionString)
            connection.Open()

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try

        Return connection
    End Function

    Private Sub Out_TranscriptBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Out_TranscriptBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Out_TranscriptBindingSource.EndEdit()
        Me.Out_TranscriptTableAdapter.Update(Me.TransferDataSet.Out_Transcript)

    End Sub

    Private Sub frmOut_Transcript_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            LoadYear()
            LoadTerm()
            Me.Out_TranscriptTableAdapter.Fill(Me.TransferDataSet.Out_Transcript)
            'Me.YearComboBox.Text = Me.YearTextBox.Text
            Me.Out_TranscriptDataGridView.Rows(0).Selected = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnLookup3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLookup3.Click
        Me.Cursor = Cursors.WaitCursor
        Dim frm As New frmLookup
        frm.LookupType = "Bulletin"

        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.CoursenoTextBox.Text = frm.CourseNo
            Me.txtTitle.Text = frm.Title
            Me.CreditTextBox.Text = frm.Credit
            'Me.txtCrelec.Text = frm.Crelec
            'Me.txtCrelab.Text = frm.Crelab
        End If
        Me.Cursor = Cursors.Default
    End Sub

    ' Display year information
    Private Sub LoadYear()
        With YearComboBox
            .Items.Add("")
            .Items.Add((DateTime.Today.Year - 4) + 543)
            .Items.Add((DateTime.Today.Year - 3) + 543)
            .Items.Add((DateTime.Today.Year - 2) + 543)
            .Items.Add((DateTime.Today.Year - 1) + 543)
            .Items.Add((DateTime.Today.Year) + 543)
            .Items.Add((DateTime.Today.Year + 1) + 543)
            .Items.Add((DateTime.Today.Year + 2) + 543)
            .Items.Add((DateTime.Today.Year + 3) + 543)
            .Items.Add((DateTime.Today.Year + 4) + 543)
        End With
    End Sub

    ' Displays term information
    Private Sub LoadTerm()
        With TermComboBox
            .Items.Add("")
            .Items.Add(1)
            .Items.Add(2)
            .Items.Add(3)
        End With
    End Sub

    Private Sub btnId_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnId.Click
        Me.Cursor = Cursors.WaitCursor
        Dim frm As New frmLookup
        frm.LookupType = "Master"

        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.IdTextBox.Text = frm.ID

            'Me.CoursenoTextBox.Text = frm.CourseNo
            'Me.txtTitle.Text = frm.Title
            'Me.txtCredit.Text = frm.Credit
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub IdTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdTextBox.TextChanged
        If Me.IdTextBox.Text.Length = 0 Then Exit Sub

        'SqlDataReader that will hold the returned results		
        Dim dr As SqlClient.SqlDataReader = Nothing

        'SqlConnection that will be used to execute the sql commands
        Dim connection As SqlClient.SqlConnection = Nothing

        Try
            Try
                connection = GetConnection()
            Catch ex As Exception
                MessageBox.Show("The connection with the database canดt be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try

            ' Set up parameters 
            Dim arParms() As SqlParameter = New SqlParameter(7) {}

            ' @Id Input Parameter  
            arParms(0) = New SqlParameter("@id", SqlDbType.Int)
            arParms(0).Value = Me.IdTextBox.Text.ToString()

            ' @prename Output Parameter  
            arParms(1) = New SqlParameter("@prename", SqlDbType.NVarChar, 20)
            arParms(1).Direction = ParameterDirection.Output

            ' @name Output Parameter  
            arParms(2) = New SqlParameter("@name", SqlDbType.NVarChar, 18)
            arParms(2).Direction = ParameterDirection.Output

            ' @sname Output Parameter  
            arParms(3) = New SqlParameter("@sname", SqlDbType.NVarChar, 21)
            arParms(3).Direction = ParameterDirection.Output

            ' @faculty Output Parameter 
            arParms(4) = New SqlParameter("@faculty", SqlDbType.NVarChar, 35)
            arParms(4).Direction = ParameterDirection.Output

            arParms(5) = New SqlParameter("@major", SqlDbType.NVarChar, 2)
            arParms(5).Direction = ParameterDirection.Output

            arParms(6) = New SqlParameter("@major_name", SqlDbType.NVarChar, 100)
            arParms(6).Direction = ParameterDirection.Output

            'Call ExecuteNonQuery static method of SqlHelper class
            'We pass in database connection string, command type, stored procedure name and an array of SqlParameter objects

            SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "getMaster", arParms)

            If arParms(1).Value.ToString = "" Then
                Exit Sub
            Else
                Me.txtFullName.Text = String.Format("{0}", arParms(2).Value.ToString() + " " + arParms(3).Value.ToString())

                'Dim sDesc As String = String.Format("{0} {1} {2}", arParms(1).Value, arParms(2).Value, arParms(3).Value)

                'Me.PrenameTextBox.Text = arParms(1).Value.ToString()
                'Me.NameTextBox.Text = arParms(2).Value.ToString()
                'Me.SnameTextBox.Text = arParms(3).Value.ToString()

                'Dim sFaculty As String = arParms(4).Value.ToString()
                'Dim sMajor As String = arParms(5).Value.ToString()
                'Dim sMajor_Name As String = arParms(6).Value.ToString()

                'Me.txtDesc.Text = PrenameTextBox.Text + " " + NameTextBox.Text + " " + SnameTextBox.Text
                'Me.txtFaculty.Text = sFaculty
                'Me.txtMajor_Id1.Text = sMajor
                'Me.txtMajor.Text = sMajor_Name
            End If

            ''iterate through SqlDataReader
            'While dr.Read()
            '    'get the value of second column in the datareader (product description)
            '    'txtResults.Text = txtResults.Text + dr.GetValue(1) + Environment.NewLine
            '    MessageBox.Show(dr.GetValue(1))
            'End While

        Catch ex As Exception
            Dim errMessage As String = ""
            Dim tempException As Exception = ex

            While (Not tempException Is Nothing)
                errMessage += tempException.Message + Environment.NewLine + Environment.NewLine
                tempException = tempException.InnerException
            End While

            MessageBox.Show(String.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}" _
              + Environment.NewLine + "This test requires some modifications to the Transfer database.", errMessage), _
              "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not dr Is Nothing Then
                CType(dr, IDisposable).Dispose()
            End If

            If Not connection Is Nothing Then
                CType(connection, IDisposable).Dispose()
            End If
        End Try
    End Sub

    Private Sub CoursenoTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CoursenoTextBox.TextChanged
        If CoursenoTextBox.Text.Length = 0 Then Exit Sub

        'SqlDataReader that will hold the returned results		
        Dim dr As SqlClient.SqlDataReader = Nothing

        'SqlConnection that will be used to execute the sql commands
        Dim connection As SqlClient.SqlConnection = Nothing

        Try
            Try
                connection = GetConnection()
            Catch ex As Exception
                MessageBox.Show("The connection with the database can't be established", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End Try

            ' Set up parameters (1 input and 3 output) 
            Dim arParms() As SqlParameter = New SqlParameter(2) {}

            ' @Id Input Parameter  
            arParms(0) = New SqlParameter("@coursno", SqlDbType.NVarChar, 6)
            arParms(0).Value = CoursenoTextBox.Text.ToString

            ' @prename Output Parameter  
            arParms(1) = New SqlParameter("@title", SqlDbType.NVarChar, 30)
            arParms(1).Direction = ParameterDirection.Output

            ' @name Output Parameter  
            arParms(2) = New SqlParameter("@credit", SqlDbType.NVarChar, 15)
            arParms(2).Direction = ParameterDirection.Output

            'Call ExecuteNonQuery static method of SqlHelper class
            'We pass in database connection string, command type, stored procedure name and an array of SqlParameter objects

            SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "getBulletin", arParms)

            'If arParms(1).Value.ToString = "" Then
            '    Exit Sub
            'Else
            'Dim sDesc As String = String.Format("{0} {1} {2}", arParms(1).Value, arParms(2).Value, arParms(3).Value)

            Dim sCoursno As String = arParms(0).Value.ToString()
            Dim sTitle As String = arParms(1).Value.ToString()
            Dim sCredit As String = arParms(2).Value.ToString()

            Me.CoursenoTextBox.Text = sCoursno
            Me.txtTitle.Text = sTitle

            'Me.CreditTextBox.Text = sCredit

            'End If

        Catch ex As Exception
            Dim errMessage As String = ""
            Dim tempException As Exception = ex

            While (Not tempException Is Nothing)
                errMessage += tempException.Message + Environment.NewLine + Environment.NewLine
                tempException = tempException.InnerException
            End While

            MessageBox.Show(String.Format("There are some problems while trying to use the Data Access Application block, please check the following error messages: {0}" _
              + Environment.NewLine + "This test requires some modifications to the Transfer database.", errMessage), _
              "Application error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not dr Is Nothing Then
                CType(dr, IDisposable).Dispose()
            End If

            If Not connection Is Nothing Then
                CType(connection, IDisposable).Dispose()
            End If
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Me.IdTextBox.Focus()
        Me.txtFullName.Text = String.Empty
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("คุณแน่ใจที่จะลบรายการนี้ใช่หรือไม่?", "ลบข้อมูล", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then            
            Out_TranscriptBindingSource.RemoveCurrent()
            Out_TranscriptBindingNavigatorSaveItem_Click(sender, e)
        End If
    End Sub

    Private Sub Out_TranscriptBindingNavigatorCancelItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Out_TranscriptBindingNavigatorCancelItem.Click
        Out_TranscriptBindingSource.CancelEdit()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class